﻿asteroid.prototype.draw = function() {
	var s2 = this.size / 2;
	c.save();
	c.translate(this.x, this.y);
	c.rotate(d2g(this.angle));
	c.drawImage(asteroidImg, 0, 0, asteroidImg.width, asteroidImg.height, -s2, -s2, this.size, this.size);
	c.restore();
};

particle.prototype.draw = function() {
	c.save();
	c.fillStyle = 'rgba(' + primaryColors[this.owner.color] + ', ' + (this.lifetime / MAX_PARTICLE_TIME) + ')';
	c.translate(this.x, this.y);
	c.beginPath();
	c.arc(0, 0, 3, 0, 2 * Math.PI, false);
	c.fill();
	c.restore();
};

explosion.prototype.draw = function() {
	var pos = (15 - this.frame) * 64;
	var s2 = this.size / 2;
	c.save();
	c.translate(this.x, this.y);
	c.drawImage(this.img, pos / 256, pos % 256, 64, 64, -s2, -s2, this.size, this.size);
	c.restore();
};

infoText.prototype.draw = function() {
	c.save();
	c.translate(this.x, this.y);
	c.textAlign = 'center';
	c.fillStyle = 'rgba(' + this.color + ', ' + (this.time / this.total) + ')';
	c.fillText(this.text, 0, 0); 
	c.restore();
};

ship.prototype.draw = function() {		
	c.save();
	var gradient = c.createLinearGradient(0, -10, 0, 15);
	gradient.addColorStop(0, 'rgb(' + primaryColors[this.color] + ')');
	gradient.addColorStop(this.life / MAX_LIFE, 'rgb(' + secondaryColors[this.color] + ')');
	gradient.addColorStop(this.life / MAX_LIFE, 'rgba(' + secondaryColors[this.color] + ', 0.2)');
	c.strokeStyle = 'rgba(10, 10, 10, 0.8)';
	c.fillStyle = gradient;
	c.translate(this.x, this.y);
	c.rotate(d2g(this.angle));
	c.beginPath();
	c.moveTo(0, -13);
	c.lineTo(10, 12);
	c.lineTo(-10, 12);
	c.closePath();
	c.stroke();
	c.fill();
	
	if(this.ammo > 0) {
		gradient = c.createLinearGradient(-10, 0, 10, 0);
		gradient.addColorStop(0, '#4682b4');
		gradient.addColorStop(this.ammo / MAX_AMMO, '#1e90ff');
		gradient.addColorStop(this.ammo / MAX_AMMO, '#FFFFFF');
		c.fillStyle = gradient;
		c.fillRect(-10, 18, 20, 3);
	}
	
	c.restore();
};

var draw = function() {
	var w = c.canvas.width,
		h = c.canvas.height,
		i;
		
	c.clearRect(0, 0, w, h);
	
	for(i = explosions.length; i--; )
		explosions[i].draw();
	
	for(i = ships.length; i--; )
		ships[i].draw();
		
	for(i = particles.length; i--; )
		particles[i].draw();
		
	for(i = asteroids.length; i--; )
		asteroids[i].draw();
	
	for(i = infoTexts.length; i--; )
		infoTexts[i].draw();
};